<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="se" uri="http://zc.security/acl"%>
<script type="text/javascript" src="<c:url value='/js/plug/area/load_area.js'/>"></script>


<div id="cc_providers" class="easyui-layout" style="width:100%;height:100%;" fit="true">
	<div data-options="region:'north',title:'供应商|货主',split:true" style="height:70px;background-color:#e6edfd;">
    	<form action="" id="searchForm" name="searchForm">
    	<table class="screen">
			<tr>
				<td class="screen-n"><span>中文名称</span></td>
				<td class="screen-n">
					<input type="text" id="s_chsName" class="screen-input" style="width:200px"onkeydown="pms.kd13('initProviders')">
				</td>
				<td class="screen-n"><span>是否存在登录账号</span></td>
				<td class="screen-n">
				    <select name="isMember" id="isMember" style="width:80px;" class="easyui-combobox" panelheight="auto">
				       <option value="">全部</option>
					   <option value="0">否</option>
					   <option value="1">是</option>
				    </select>
				</td>
				<td class="screen-n"><span>所属地区</span></td>
				<td class="screen-n">
						<div id="s_loc" >
     		                  <select name="province" id="province" style="width:100px;">
     		                     <option value="0">选择省</option>
     		                  </select>   
     		                  <select name="city" id="city" style="width:100px;">
     		                     <option value="0">选择市</option>
     		                  </select>   
     		                  <select name="district" id="district" style="width:100px;">
     		                     <option value="0">选择地区</option>
     		                  </select>
     	                  </div>
				</td>
				<td class="screen-n">
					<input type="button" class="button" value="查询" onclick="initProviders()">
					<input type="button" class="button" value="导出" onclick="loadExecl('initStoreOutCheckTableExcel')" >
				</td>
				<td class="screen-n">
					批量设置开始查询时间:
					<input type="text" id="startRq" class="screen-input easyui-datebox" onkeydown="pms.kd13('batchUpdateProvider')">
					<input type="button" class="button" value="设置" onclick="batchUpdateProvider()">
				</td>
			</tr>
		</table>
		</form>
    </div>
    <div data-options="region:'center',split:true" style="width: 100%;height: 100%">
		<table id="tableGrid"></table>
	</div>
</div>  
<script>

var toolbar = [
<se:hasPermissions actCode="ADD" >
{
	text:'供应商录入',
    iconCls:'ext-icon-note_add',
    handler:function(){
    	openProviderAddWindow();
    }
},
</se:hasPermissions>
<se:hasPermissions actCode="APPROVE" >
{
	text:'供应商审核',
    iconCls:'ext-icon-key',
    handler:function(){
    	var rows = providersTable.getCheckedData();
		if(rows==null || rows.length==0){
			alertWarning("请选中要审核的数据!");
			return;
		}

		//供应商审核
		if(confirm("确定批量审核?")){
			var ids = [];
			for(var i=0; i<rows.length; i++){
				ids.push(rows[i].providerId);
			}
			var providerIdList = ids.toString();
			var param = {"providerIdList":providerIdList};
			$.pmsAjax({
				url:'<c:url value="/emcprovider/editMultipleEmCProvidersStatus.do"/>',
				data:param,
				success:function(data){
					if(data.code=="200"){
						//console.log(data);
						alertShow("批量审批成功！");
						providersTable.refresh();
					}else{
						alertError(data.result);
					}
				}
			});
		}
    }
},
</se:hasPermissions>
<se:hasPermissions actCode="DELETE" >
{
	text:'供应商删除',
    iconCls:'ext-icon-note_delete',
    handler:function(){
    	var rows = providersTable.getCheckedData();
		if(rows==null || rows.length==0){
			alertWarning("请选中要删除的数据!");
			return;
		}
		if(confirm("确定批量删除?")){
			var ids = [];
			for(var i=0; i<rows.length; i++){
				ids.push(rows[i].providerId);
			}
			var providerIdList = ids.toString();
			var param = {"providerIdList":providerIdList};
			$.pmsAjax({
				url:'<c:url value="/emcprovider/deleteMultipleEmCProviders.do"/>',
				data:param,
				success:function(data){
					if(data.code=="200"){
						alertShow("批量删除成功！");
						providersTable.refresh();
					}else{
						alertError(data.result);
					}
				}
			});
		}
    }
}
</se:hasPermissions>
];
var providersTable;
$(function(){
	initProviders();
});
function getParams(){
	var queryParamss = {"chsName":$('#s_chsName').val(), 
      	    "isMember":$('#isMember').combobox('getValue'),
	        "areaCode":$('#s_loc').getLocation('all').code
	       };
	return queryParamss;
}
var queryParamss;
function initProviders(){
	providersTable = $('#tableGrid').pDataGrid({
		toolbar:toolbar,
		columns:[[
				   {field:'ck',checkbox:true},
				{field:'action', width : 20,title : '操作',align : 'center',
						formatter : function(value, row, index) {
							var str = '';
							<se:hasPermissions actCode="UPDATE" >
							str += ' <img class="iconImg ext-icon-note_edit" title="&nbsp;&nbsp;编辑" onclick="openProviderUpdateWindow('+row.providerId+');"/>';
							</se:hasPermissions>
							<se:hasPermissions actCode="MAINTAIN" >
								str += ' <img class="iconImg ext-icon-group_add" title="&nbsp;&nbsp;添加登录账号" onclick="AddProviderUserWindow('+row.providerId+');"/>';
/* 								str += ' <img class="iconImg ext-icon-user_add" title="&nbsp;&nbsp;设置所属客户" onclick="fetchBelongBuyer(\''+row.providerCode+'\');"/>';
 */						    	str += ' <img class="iconImg ext-icon-basket_add" title="&nbsp;&nbsp;设置所属商品" onclick="fetchBelongProduct(\''+row.providerCode+'\');"/>';  	
				    		</se:hasPermissions>
							<se:hasPermissions actCode="DELETE" >
							str += ' <img class="iconImg ext-icon-note_delete" title="&nbsp;&nbsp;删除" onclick="deleteProvider('+row.providerId+',' + index + ');"/>';
							</se:hasPermissions>
						    	return str;
						}
					} ,
	               {field:'chsName', title:'供货商名称',align:'center'},
	               {field:'providerCode',title:'供应商编号',align:'center'},
	               {field:'spell',title:'拼音码',align:'center'},
	               {field:'phone',title:'电话',align:'center'},
	               {field:'mobile',title:'手机',align:'center'},
	               {field:'consigner',title:'委托人',align:'center'},
/* 	               {field:'productionScope',title:'生产经营范围',align:'center'}, */
	               {field:'levelId',title:'会员等级',align:'center',formatter:function(value){
						if(value=="1"){
							return '普通会员';
						}else{
							return '高级会员';
						}
		           }},
		           {field:'areaName',title:'地区',align:'center'},
	               {field:'address',title:'地址',align:'center'},
	               {field:'approveStatus',title:'是否审核',align:'center',formatter:function(value){
						if(value=="0"){
							return '未审';
						}else{
							return '审核';
						}
		           }}
		          ]],
		    url:'<c:url value="/emcprovider/searchPageProvider.do" />',  
		    pagination:true,//分页控件
		    pageSize:30,
		    singleSelect:false,
		    rownumbers:true,
		    fitColumns:true,
		    queryParams:getParams(),
			onDblClickRow:function(index,row){
				openProviderUpdateWindow(row.providerId);
			},
			onLoadSuccess : function(data) {
				var panel = $(this).datagrid('getPanel');
				var tr = panel.find('div.datagrid-body tr');
				tr.each(function() {
					var td = $(this).children('td[field="chsName"]');
					td.children("div").css({
						"text-align" : "left"
					});
				});
				tr.each(function() {
					var td = $(this).children('td[field="productionScope"]');
					td.children("div").css({
						"text-align" : "left"
					});
				});
				tr.each(function() {
					var td = $(this).children('td[field="areaName"]');
					td.children("div").css({
						"text-align" : "left"
					});
				});
				tr.each(function() {
					var td = $(this).children('td[field="address"]');
					td.children("div").css({
						"text-align" : "left"
					});
				});
			}
		    
	}); 
}

function openProviderAddWindow(){
	$.pmsDialog({
		id:"w_provider_add",
		title:'新增供应商',
		isIf:'true',
		method:'post',
		href:'<c:url value="/views/jsp/customer/provider/provider_detail.jsp"/>',
		width:890
	});
}
function openProviderUpdateWindow(code){
	var params = {"code":code};
	$.pmsDialog({
		id:"w_provider_add",
		title:'修改供应商',
		isIf:'true',
		method:'post',
		params:params,
		href:'<c:url value="/views/jsp/customer/provider/provider_detail.jsp"/>',
		width:890
	}); 
}
//获取归属客户列表
function fetchBelongBuyer(providerCode){
	var params = {"providerCode":providerCode};
	$.pmsDialog({
		id:"w_provider_belong_buyer_list",
		title:'已添加所属客户',
		isIf:'true',
		method:'post',
		params:params,
		href:'<c:url value="/views/jsp/customer/provider/provider_belong_buyer_list.jsp"/>',
		width:1000
	}); 
}

//获取归属客户列表
function fetchBelongProduct(providerCode){
	var params = {"providerCode":providerCode};
	$.pmsDialog({
		id:"w_provider_product_list",
		title:'已添加所属商品',
		isIf:'true',
		method:'post',
		params:params,
		href:'<c:url value="/views/jsp/customer/provider/provider_product_list.jsp"/>',
		width:800
	}); 
}
function AddProviderUserWindow(code){
	var params = {"code":code};
	$.pmsDialog({
		id:"w_adduser",
		title:'添加登录账号',
		method:'post',
		params:params,
		href:'<c:url value="/views/jsp/customer/provider/provider_adduser_s.jsp"/>',
		width:550,
		height:550
	}); 
}
function deleteProvider(code,i){
	if(confirm("确定删除?")){
		var param = {"providerId":code};
		$.pmsAjax({
			url:'<c:url value="/emcprovider/deleteProvider.do"/>',
			data:param,
			success:function(data){
				if(data.code=="200"){
					alertShow("删除成功");
					providersTable.refresh();
				}else{
					alertError(data.result);
				}
			}
		});
	}
}
function batchUpdateProvider(){
	
 	var row = $('#tableGrid').datagrid('getSelections');
	if(row.length==0){
		alertWarning("您未选择任何商品!");
		return;
	} 
	alert($("#startRq").datebox("getValue"));
	var searchStartDate = $("#startRq").datebox("getValue");
	if(searchStartDate==null){
		alertWarning("您未设置日期!");
		return;
	}
	var param = new Object();
	for(var i=0;i<row.length;i++){
		param['list['+i+'].providerId']=row[i].providerId;
		param['list['+i+'].searchStartDate']=$("#startRq").datebox("getValue");
	}
	$.pmsAjax({
		url:'<c:url value="/emcprovider/batchUpdateProvider.do"/>',
		data:param,
		success:function(data){
			if(data.code=="200"){
				alertShow("批量操作成功");
			}else{
				alertError(data.result);
			}
		}
	});
}

$("#s_loc").LocationSelect({
	data:"<c:url value='/js/plug/area/areas.json' />"
});
//导出
function initStoreOutCheckTableExcel(){
	loadExcelCallBack('<c:url value="/emcprovider/searchPageProviderExcel.do"/>',getParams());
} 	
</script>
